package com.example.shop_2210.login

import android.content.Intent
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.BackgroundColorSpan
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop_2210.R
import com.example.shop_2210.databinding.ActivityLoginBinding
@Route(path = "/app/LoginActivity")
class LoginActivity : BaseActivity<ActivityLoginBinding,LoginViewModel>() {
    override fun initData() {
        viewModel.loginSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                //登录成功 保存token 和手机号 方便后续修改密码
                SPUtils.getInstance().put("token",it.data.token)
                SPUtils.getInstance().put("phone",it.data.phone)


                ARouter.getInstance().build("/app/ShopActivity").navigation()
            }
        }
    }

    override fun initView() {
        //富文本
        val spannableStringBuilder = SpannableStringBuilder(binding.tvFwb.text.toString())
        spannableStringBuilder.setSpan(ForegroundColorSpan(Color.RED),2,4,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)//文字颜色
        spannableStringBuilder.setSpan(BackgroundColorSpan(Color.GREEN),2,4,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)//背景颜色
        val clickableSpan = object:ClickableSpan(){
            override fun onClick(widget: View) {
                //可吐司  可跳转
                ToastUtils.showLong("ssss")
                ARouter.getInstance().build("/app/WebActivity").withString("url","http://www.baidu.com").navigation()
            }
        }
        spannableStringBuilder.setSpan(clickableSpan,6,8,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)//点击事件
        binding.tvFwb.movementMethod = LinkMovementMethod.getInstance()//移除本身的点击事件
        binding.tvFwb.text = spannableStringBuilder




        //跳转到验证码登录页面
        binding.goLoginCode.setOnClickListener {
            val intent = Intent(this,LoginCodeActivity::class.java)
            startActivity(intent)
            finish()
        }
        //跳转到修改密码界面---修改密码成功后，回传用户名和密码
        binding.toUpdate.setOnClickListener {
            val intent = Intent(this,PasswordActivity::class.java)
            startActivityForResult(intent,102)
            //ARouter.getInstance().build("//").navigation(this,102)
        }
        //点击登录
        binding.btnLogin.setOnClickListener {
           /* if(!RegexUtils.isMatch("^[a-zA-z][\\w]{5,17}$",binding.etPassword.text.toString())){
                ToastUtils.showLong("密码格式不正确，应为6-18位的数字、字母、下划线组合，其中用字母开头")
            }*/
            if(!RegexUtils.isMatch("^[\\w]{6,18}$",binding.etPassword.text.toString())){
                ToastUtils.showLong("密码格式不正确，应为6-18位的数字、字母、下划线组合")
                return@setOnClickListener
            }
            viewModel.login(binding.etPhone.text.toString(),binding.etPassword.text.toString())
        }
    }

    override fun getViewModel(): Class<LoginViewModel>  = LoginViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_login
    //接收修改密码页面的回传值
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==102&&resultCode==103){
            val phone = data?.extras?.getString("phone")
            val password = data?.extras?.getString("password")
            binding.etPhone.setText(phone)
            binding.etPassword.setText(password)
        }
    }

}